package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.R;
import com.example.entity.AddressBook;
import com.example.dao.AddressBookDao;
import com.example.service.AddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地址管理(AddressBook)表服务实现类
 *
 * @author makejava
 * @since 2022-05-27 10:41:15
 */
@Service("addressBookService")
public class AddressBookServiceImpl extends ServiceImpl<AddressBookDao,AddressBook> implements AddressBookService {
    @Resource
    private AddressBookDao addressBookDao;
    @Resource
    private AddressBookServiceImpl addressBookService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AddressBook queryById(Long id) {
        return this.addressBookDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<AddressBook> queryAllByLimit(int offset, int limit) {
        return this.addressBookDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param addressBook 实例对象
     * @return 实例对象
     */
    @Override
    public AddressBook insert(AddressBook addressBook) {
        this.addressBookDao.insert(addressBook);
        return addressBook;
    }

    /**
     * 修改数据
     *
     * @param addressBook 实例对象
     * @return 实例对象
     */
    @Override
    public AddressBook update(AddressBook addressBook) {
        this.addressBookDao.update(addressBook);
        return this.queryById(addressBook.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.addressBookDao.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean updateServiceDefault(Long id,Long user_ID) {
        int a=addressBookDao.updatedefault(user_ID);
        System.out.println(a);
        if (a>0){
            LambdaQueryWrapper<AddressBook>queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(AddressBook::getId,id);
            AddressBook addressBook=addressBookService.getOne(queryWrapper);
            addressBook.setIsDefault(1);
            addressBookService.updateById(addressBook);
            return true;
        }
        return false;
    }

}